import axios from 'axios'
import router from '@/router/index'
import Global from "../components/Global";

// 创建axios实例
const service = axios.create({
  baseURL: Global.serverSrc, // api 的 base_url
  timeout: 60000 // 请求超时时间
})

// request拦截器
service.interceptors.request.use(
  config => {
    const token = window.localStorage.getItem('token');
    config.headers['token'] = token;
    config.headers['Content-Type'] = 'application/json'
    return config
  },
  error => {
    Promise.reject(error)
  }
)

// response 拦截器
service.interceptors.response.use(
  response => {
      return response.data
    },
      error => {
        let code = 0
        try {
          code = error.response.status
        } catch (e) {

        }
        if (code) {
          if (code === 401) {
            window.location.href = "/#/login"
          }
        }
        return Promise.reject(error)
  }
)
export default service
